package com.sky.cqculture.Contraller;

import com.sky.cqculture.pojo.Result;
import com.sky.cqculture.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/favorites")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 切换收藏状态
     */
    @PostMapping("/toggle")
    public Result toggleFavorite(@RequestBody Map<String, Object> params) {
        try {
            // 从请求体中获取参数
            Integer userId = (Integer) params.get("userId");
            Integer heritageId = (Integer) params.get("heritageId");
            Integer status = (Integer) params.get("status");

            // 参数验证
            if (userId == null || heritageId == null || status == null) {
                return Result.error("参数不完整");
            }

            // 执行收藏/取消收藏操作
            favoriteService.toggleFavorite(userId, heritageId, status);
            return Result.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }

    /**
     * 检查用户是否已收藏某个非遗项目
     */
    @GetMapping("/check")
    public Result checkFavorite(@RequestParam Integer userId, @RequestParam Integer heritageId) {
        try {
            boolean isFavorite = favoriteService.isFavorite(userId, heritageId);
            return Result.success(isFavorite);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 获取用户收藏的所有非遗项目
     */
    @GetMapping("/user/{userId}")
    public Result getUserFavorites(@PathVariable Integer userId) {
        try {
            return Result.success(favoriteService.getUserFavorites(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }
}